package com.wlyuan.gateway.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.Optional;

/**
 * @author c3
 */
@RestController
@RequestMapping("")
public class ResourceController {
    @Autowired(required = false)
    private UiConfiguration ui;
    @Autowired(required = false)
    private SecurityConfiguration security;
    private final SwaggerResourcesProvider resources;

    @Autowired
    public ResourceController(SwaggerResourcesProvider resources) {
        this.resources = resources;
    }


    @GetMapping("/swagger-resources/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> security() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(security).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/swagger-resources/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> ui() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(ui).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/swagger-resources")
    public Mono<ResponseEntity<?>> resources() {
        return Mono.just((new ResponseEntity<>(resources.get(), HttpStatus.OK)));
    }
}